﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class value : System.Web.UI.Page
    {
        Maticsoft.BLL.valuetype bll = new Maticsoft.BLL.valuetype();
        Maticsoft.BLL.InventoryClass classbll = new Maticsoft.BLL.InventoryClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((bll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "getvalue")
                {
                    List<Maticsoft.Model.InventoryClass> classlist = classbll.GetModelList("cinvccode like '05%' and cinvcname='" + Request["cInvcName"] + "'");
                    List<Maticsoft.Model.valuetype> list = bll.GetModelList("vtype=1 or cInvCCodes='" + classlist[0].cInvCCode + "'");
                    string ss = JsonConvert.SerializeObject(new { tongyong = list.Where(c => c.vtype == 1).Select(c => new { tongyongname = c.name, select = c.parentcode, code = c.code, tongyonglist = c.value.Split(',') }), guanlian = list.Where(c => c.vtype == 2).Select(c => new { tongyongname = c.name, select = c.parentcode, code = c.code, tongyonglist = c.value.Split(',') }) });
                    Response.Write(ss);
                }
                else
                {
                    Maticsoft.Model.valuetype md = new Maticsoft.Model.valuetype();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        md = bll.GetModel(Convert.ToInt32(Request["id"]));
                        md.note = Request["note"];
                        md.name = Request["name"];
                        md.parentcode = Request["parentlist"] == "" ? "0" : Request["parentlist"];
                        md.code = Request["code"];
                        md.value = Request["value"];
                        md.vtype = Convert.ToInt32(Request["vtype"]);
                        md.cInvCCodes = Request["cInvCCodes"];
                        row = bll.Update(md) ? 2 : 0;
                    }
                    else
                    {
                        md.note = Request["note"];
                        md.name = Request["name"];
                        md.parentcode = Request["parentlist"] == "" ? "0" : Request["parentlist"];
                        md.code = Request["code"];
                        md.value = Request["value"];
                        md.vtype = Convert.ToInt32(Request["vtype"]);
                        md.cInvCCodes = Request["cInvCCodes"];
                        row = bll.Add(md) > 0 ? 1 : 0;
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }
                    else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}